//
//  NSString+MoneyString.m
//  xieyingze
//
//  Created by xieyingze on 15/12/7.
//  Copyright © 2015年 xieyingze. All rights reserved.
//

#import "NSString+MoneyString.h"

@implementation NSString (MoneyString)

- (NSString *)moneyString {
//    return self;
    if (self == nil) return nil;
    if ([self  isEqual: @""]) return @"";//为空返回
    
    NSString *numStr = [[NSString stringWithFormat:@"%@",self] moneyStringToNumber];
//    numStr = [NSString stringWithFormat:@"%.2f",[numStr doubleValue]];
    NSString *signStr = @"";
    if ([numStr hasPrefix:@"+"] || [numStr hasPrefix:@"-"]) {//处理字符串中的正负号
        signStr = [numStr substringToIndex:1];
        numStr = [numStr substringFromIndex:1];
    }
    NSArray *separeArray = [numStr componentsSeparatedByString:@"."];
    NSString *intStr = [separeArray firstObject];
    NSInteger length = intStr.length;
    if (!length) return self;
    NSInteger front = length%3;
    NSInteger count = length/3;
    NSMutableString *temp = [NSMutableString string];
    [temp appendString:[NSString stringWithFormat:@"%@",[intStr substringToIndex:front]]];
    for (int i = 0; i < count; i ++) {
        [temp appendString:[NSString stringWithFormat:@",%@",[intStr substringWithRange:(NSRange){front + i * 3,3}]]];
    }
    if (front)//没有整除
    {
        //没有小数点
        if (separeArray.count == 1)   return [NSString stringWithFormat:@"%@%@",signStr,temp];
        //有小数点
        else                          return [NSString stringWithFormat:@"%@%@.%@",signStr,temp,[separeArray lastObject]];
    }
    else//整除，前面多了一个  ,
    {
        //没有小数点
        if (separeArray.count == 1)   return [NSString stringWithFormat:@"%@%@",signStr,[temp substringFromIndex:1]];
        //有小数点
        else                          return [NSString stringWithFormat:@"%@%@.%@",signStr,[temp substringFromIndex:1],[separeArray lastObject]];
    }
}
- (NSString *)moneyStringToNumber {
    return [[self componentsSeparatedByString:@","] componentsJoinedByString:@""];
}

/**
 *  分隔手机号码
 *
 *  @return 分隔后的手机号码
 */
- (NSString *)phoneSeparateString
{
    NSMutableString *phone = [NSMutableString stringWithCapacity:11];
    
    if (self.length <= 3) {
        [phone appendString:self];
    }else if (self.length > 3 && self.length <= 3+4){
        [phone appendFormat:@"%@-%@", [self substringToIndex:3], [self substringFromIndex:3]];
    }else if (self.length > 3+4 && self.length <= 11){
        [phone appendFormat:@"%@-%@-%@", [self substringToIndex:3], [self substringWithRange:NSMakeRange(3, 4)], [self substringFromIndex:3+4]];
    }
    return phone;
}

- (NSString *)phoneString
{
    if (self.length != 11) return self;
    NSMutableString *phone = [NSMutableString stringWithString:self];
    [phone replaceCharactersInRange:NSMakeRange(3, 6) withString:@"******"];
    return phone;
}

- (NSString *)nameString
{
    if (self.length <= 1) {
        return self;
    }
    NSMutableString *str = [NSMutableString stringWithString:self];
    for (int i = 0; i < str.length; i++) {
        if (i == 0) continue;
        [str replaceCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
    }
    return str;
}

- (NSString *)showFrontAndBackFour
{
    if (self.length <8) {
        return self;
    }
    NSMutableString *str = [NSMutableString stringWithString:self];
    for (int i = 0; i < str.length; i ++) {
        if (i<4 || i > str.length-4-1) {
            continue;
        }
        [str replaceCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
    }
    return str;
}
- (NSString *)keepTwoDecimal {
    NSString *tempSelf = self;
    if ([self isKindOfClass:[NSNull class]]) tempSelf = @"";
    return [NSString stringWithFormat:@"%.2f",[[tempSelf moneyStringToNumber] doubleValue]];
}

- (NSString *)bankCardNumberString
{
    return [NSString stringWithFormat:@"%@ **** **** %@",[self substringToIndex:4],[self substringFromIndex:self.length - 3]];
}

@end
